package com.ys.web.background.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import com.ys.base.BaseConstants;
import com.ys.base.EnumType;
import com.ys.utils.base.http.McodeHandler;
import com.ys.utils.base.j2se.DateUtil;
import com.ys.utils.base.j2se.Logger;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.help.GroupUtils;
import com.ys.utils.help.UserUtils;
import com.ys.web.background.mapper.BackgroundUserMapper;
import com.ys.web.background.service.BackgroundUserService;
import com.ys.web.ws.bean.MessageResponseBean;
import com.ys.web.ws.manager.ChatObjectManager;
import io.vertx.core.http.ServerWebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ 名称:
 * @ 创建人: dhf
 * @ 参数： null
 * @ 创建时间: 2022/5/10
 * @ 修改人和其它信息
 * @ 返回值:
 */
@Slf4j
@Service
public class BackgroundUserServiceImpl implements BackgroundUserService {

    @Autowired
    private BackgroundUserMapper backgroundUserMapper;

    @Override
    public void updateMiNingUserStatus(String id, int status) {
        backgroundUserMapper.updateUserStatus(id, status);
        if (status == 2) {
            ThreadUtil.execAsync(() -> {
                ServerWebSocket serverWebSocket = ChatObjectManager.getConnectionMap().get(McodeHandler.to32Radix(Long.parseLong(id)));
                if (StringHandler.isNotEmpty(serverWebSocket)) {
                    String str = UserUtils.getMessageResponseList(id, null,
                            EnumType.MessageCode.SYSTEM_ACCOUNT_DISABLE.getCode(),
                            EnumType.MessageCode.SYSTEM_ACCOUNT_DISABLE.getDesc(),
                            DateUtil.getDate(new Date(), BaseConstants.BASE_FORMAT));
                    Logger.info("推送信息：" + str);
                    serverWebSocket.writeTextMessage(str);
                }else {
                    MessageResponseBean messageResponseBean = UserUtils.getMessageResponse(id, null, EnumType.MessageCode.SYSTEM_ACCOUNT_DISABLE.getCode(), null, null, 0.0, 0.0);
                    GroupUtils.sendIfAbsent(messageResponseBean, id, null);
                }
            });
        }
    }

    @Override
    public void updateMiNingUserDeleted(String id, int status) {
        backgroundUserMapper.updateUserDeleted(id, status);
        if (status == 1) {
            ThreadUtil.execAsync(() -> {
                ServerWebSocket serverWebSocket = ChatObjectManager.getConnectionMap().get(McodeHandler.to32Radix(Long.parseLong(id)));
                if (StringHandler.isNotEmpty(serverWebSocket)) {
                    String str = UserUtils.getMessageResponseList(id, null,
                            EnumType.MessageCode.SYSTEM_ACCOUNT_FREEZE.getCode(),
                            EnumType.MessageCode.SYSTEM_ACCOUNT_FREEZE.getDesc(),
                            DateUtil.getDate(new Date(), BaseConstants.BASE_FORMAT));
                    serverWebSocket.writeTextMessage(str);
                }else {
                    MessageResponseBean messageResponseBean = UserUtils.getMessageResponse(id, null, EnumType.MessageCode.SYSTEM_ACCOUNT_FREEZE.getCode(), null, null, 0.0, 0.0);
                    GroupUtils.sendIfAbsent(messageResponseBean, id, null);
                }
            });
        }
    }
    @Override
    public void updateMiNingFh(String id, int status) {
        backgroundUserMapper.updateUserDeleted(id, status);
        if (status == 2) {
            ThreadUtil.execAsync(() -> {
                ServerWebSocket serverWebSocket = ChatObjectManager.getConnectionMap().get(McodeHandler.to32Radix(Long.parseLong(id)));
                if (StringHandler.isNotEmpty(serverWebSocket)) {
                    String str = UserUtils.getMessageResponseList(id, null,
                            EnumType.MessageCode.SYSTEM_ACCOUNT_DISABLE.getCode(),
                            EnumType.MessageCode.SYSTEM_ACCOUNT_DISABLE.getDesc(),
                            DateUtil.getDate(new Date(), BaseConstants.BASE_FORMAT));
                    serverWebSocket.writeTextMessage(str);
                }else {
                    MessageResponseBean messageResponseBean = UserUtils.getMessageResponse(id, null, EnumType.MessageCode.SYSTEM_ACCOUNT_DISABLE.getCode(), null, null, 0.0, 0.0);
                    GroupUtils.sendIfAbsent(messageResponseBean, id, null);
                }
            });
        }
    }

}
